<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      :disabled="message.length===0"
      @click="submit"
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'CommentPost',
  data () {
    return {
      message: ''
    }
  },
  inject:['articleId'],
  props:{
    // 文章id或者评论id
    target:{
      type:[String,Number],
      required:true
    },
    // 判断操作类型
    type:{
      default:'a', 
      validator(val){
        return ['a','c'].includes(val)
      }
    }
  },
  methods: {
    async submit(){
      try {
        let { data } = await addComment({
          target:this.target,  // 文章id或评论id
          content:this.message, // 内容
          art_id:this.type ==='c' ? this.articleId : null  // 文章id，对评论进行回复的时候才需要
        })
        this.message = ''
        this.$emit('update-list',data.data)
      } catch (error) {
        console.log(error);
        this.$toast.fail('发布失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>